package com.fate.system.utils;

import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.IOException;
import java.io.InputStream;

public class FilePagesUtil {
  public static int getDocPageCount(InputStream inputStream) throws IOException {
    HWPFDocument doc = new HWPFDocument(inputStream);
    WordExtractor extractor = new WordExtractor(doc);
    return extractor.getSummaryInformation().getPageCount();
  }

  public static int getDocxPageCount(InputStream inputStream) throws IOException {
    XWPFDocument docx = new XWPFDocument(inputStream);
    XWPFWordExtractor extractor = new XWPFWordExtractor(docx);
    return extractor.getExtendedProperties().getUnderlyingProperties().getPages();
  }

  public static int getXlsPageCount(InputStream inputStream) throws IOException {
    HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
    Sheet sheet = workbook.getSheetAt(0);
    return sheet.getPrintSetup().getCopies();
  }

  public static int getXlsxPageCount(InputStream inputStream) throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
    Sheet sheet = workbook.getSheetAt(0);
    return sheet.getPrintSetup().getCopies();
  }

  public static int getPptSlideCount(InputStream inputStream) throws IOException {
    HSLFSlideShow ppt = new HSLFSlideShow(inputStream);
    return ppt.getSlides().size();
  }

  public static int getPptxSlideCount(InputStream inputStream) throws IOException {
    XMLSlideShow pptx = new XMLSlideShow(inputStream);
    return pptx.getSlides().size();
  }
}
